package xyc.szk.web.servlet.request;
/*
 * @Description:
 * @Author: 山志凯
 * @Date: 2021-03-24
 * @Time: 09:28
 */

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet("/demo01")

public class DemoRequest01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doGet(req, resp);
        //req这个对象是有浏览器在请求时由Tomcat服务器创建的，它的类型为：org.apache.catalina.connector.RequestFacade
        //这个类的Tomcat提供的，它实现了HttpServletRequest这个接口，它包含了用户的请求信息
        //请求消息：请求行；请求头；请求体
        //System.out.println(req); //org.apache.catalina.connector.RequestFacade@75aaeeb9
        //获取请求消息
        //请求行：
        System.out.println("请求行的协议：" + req.getProtocol());
        System.out.println("服务器地址：" + req.getServerName());
        System.out.println("端口号：" + req.getServerPort());
        System.out.println("项目的虚拟目录：" + req.getServletContext().getContextPath());
        System.out.println("请求的servlet的名称：" + req.getServletPath());
        System.out.println("浏览器的地址：" + req.getRemoteAddr());
        System.out.println("浏览器主机的端口：" + req.getRemotePort()); //电脑共有65536个端口
        //请求参数：请求体，只限于get请求
        System.out.println("请求的参数信息：" + req.getQueryString());
        System.out.println("请求的方式：" + req.getMethod());
        System.out.println("请求的URL：" + req.getRequestURL());
        System.out.println("请求的URI：" + req.getRequestURI());
        //请求头：key:value
        Enumeration<String> hearders = req.getHeaderNames();
        while (hearders.hasMoreElements()) {
            String key = hearders.nextElement();
            String value = req.getHeader(key);
            System.out.println(key + ":" + value);
        }
    }
}
